import { GlobalConfig } from '../../GlobalConfig';
import text_config from '../../game/entity/text_config';
import { ConfigMgr } from '../manager/ConfigMgr';
import { StringConst } from './../../game/const/StringConst';
export default class StringUtils {

    public static bold(str: string | number): string {
        return `<b>${str}</b>`
    }

    public static i(str: string | number): string {
        return `<i>${str}</i>`
    }



    /**格式化字符串,%s替换 */
    public static formatString(msg: string | any, ...subst: any[]): string {
        let arrStr = msg.split("%s");
        let resultStr = "";
        for (let i = 0; i < arrStr.length; i++) {
            resultStr = `${resultStr}${arrStr[i]}${subst[i] || ""}`;
        }
        return resultStr;
    }

    /**获取字符串长度,汉字占2个 */
    public static strLength(str: string) {
        let len = 0;
        for (let i = 0; i < str.length; i++) {
            if (str.charCodeAt(i) > 255)
                len += 2;//按照预期计数增加2
            else
                len++;
        }
        return len;
    }

    /**截断字符串一个汉字当两个 */
    public static subStrLength(str: string, len: number) {
        let count = 0;
        for (let i = 0; i < str.length; i++) {
            if (str.charCodeAt(i) > 255)
                count += 2;//按照预期计数增加2
            else
                count++;

            if (count >= len) {
                return str.substr(0, i + 1);
            }
        }
        return str;
    }


    /**
     * 根据长度格式化字符串,汉字当2个
     * @param str 原始字符串
     * @param len 保留长度,汉字占2
     * @param suffix 后缀字符
     */
    public static formatStrLen(str: string, len: number, suffix: string = "...") {
        return this.strLength(str) > len ? `${this.subStrLength(str, len)}...` : str;
    }

    /**判断是否是手机号 */
    public static isPhoneNum(phoneNumber: string) {
        if (phoneNumber == undefined) {
            return false;
        }
        if (phoneNumber.length != 11) {
            return false;
        }
        const regMOBILE = /^(13[0-9]|14[5-9]|15[0-3,5-9]|16[2,5,6,7]|17[0-8]|18[0-9]|19[0-3,5-9])\d{8}$/;
        if (regMOBILE.test(phoneNumber)) return true;
        return false;
    }

    /**判断字符是否为空 */
    public static isEmpty(obj: string) {
        if (typeof obj == "undefined" || obj == null || obj == "") {
            return true;
        } else {
            return false;
        }
    }


    /**格式化钱 */
    public static formatMoney(money: number): number {
        let arr = money.toString().split(".")
        if (arr.length > 2) {
            return Number(`${arr[0]}.${arr[1]}`)
        }
        if (arr[1] && arr[1].length > 1) {
            return Number(money.toFixed(1))
        }
        return Number(money);
    }

    /**
     * @desc 根据文本id 获取真实文本内容
     * @param stringId 字符串id
     * @param params 替换参数
     * 例: StringConst["main_ui_4"] = "带参数文本:${level}";
     * StringUtils.getStr("main_ui_4",{level:10}) 返回 "带参数文本: 10"
     * @returns 
     */
    public static getStr(stringId: string, params?: { [key: string]: string | number }) {
        let str:string = null;
        const cfg = ConfigMgr.getInstance().getConfig<text_config>("text_config",stringId);
        if (!cfg) {
            str = StringConst[stringId];
            if (!str) {
                //配置表里没有, 常量里也没有,直接返回id
                return stringId;                
            }
        }else{
            //TODO 根据不同语言读取不同的文本            
            str = cfg[GlobalConfig.defaultLanguage];            
            str = str.replace(/\\n/g, '\n');
        }
        
        if (!params) {
            return str;
        }

        for (const key in params) {
            const param = params[key];
        
            if (typeof(param) === 'number') {
                str = str["replaceAll"] ? str["replaceAll"](`\${${key}}`,param.toString()) : str.replace(`\${${key}}`,param.toString());                
            }else{
                str = str["replaceAll"] ? str["replaceAll"](`\${${key}}`,param) : str.replace(`\${${key}}`,param);
            }
        }
        return str;        
    }
}